package com.boc.startup;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.arouter.launcher.ARouter;

import java.util.ArrayList;
import java.util.List;

import demo.bocweb.com.sdk.config.ARoutConfig;

import static demo.bocweb.com.sdk.config.AppConfig.isMustLogin;

/**
 * 引导页ada
 * Created by zhelun on 2018/2/1.
 */

public class GuildPagerAdapter extends PagerAdapter {
    private List<Integer> images = new ArrayList<>();
    private Context context;
    private Activity activity;

    public GuildPagerAdapter(List<Integer> images, Context context, Activity activity) {
        this.images = images;
        this.context = context;
        this.activity = activity;

    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View v = LayoutInflater.from(context).inflate(R.layout.layout_image, null);
        final ImageView img = v.findViewById(R.id.iv_image);
        img.setImageResource(images.get(position));
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position == images.size() - 1) {
                    if (isMustLogin) {
                        ARouter.getInstance().build(ARoutConfig.loginPath).greenChannel().navigation();
                    } else {
                        ARouter.getInstance().build(ARoutConfig.homeActivityPath).greenChannel().navigation();
                    }
                    activity.finish();
                }
            }
        });
        container.addView(v);
        return v;
    }
}
